package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import com.google.gson.Gson;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.CustomerMultipleAdapter;
import com.isunland.managesystem.base.BaseListFragment;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.entity.CustomerMainOriginal;
import com.isunland.managesystem.utils.LogUtil;
import com.isunland.managesystem.utils.ToastUtil;
import java.util.HashMap;
import java.util.List;

/**
 * Created by lenovo on 2016/4/22.
 */
public class CustomerMultipleListFragment extends BaseListFragment {
  protected static final String EXTRA_ID =
      "com.isunland.managesystem.ui.CustomerMultipleListFragment.EXTRA_ITEM";
  protected static final String EXTRA_NAME =
      "com.isunland.managesystem.ui.CustomerMultipleListFragment.EXTRA_NAME";
  private CustomerMultipleAdapter mAdapter;

  @Override public void onCreate(Bundle savedInstanceState) {
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.customer_namelist);
    super.onCreate(savedInstanceState);
  }

  @Override public String getUrl() {
    return ApiConst.URL_CUSTOMER_LIST;
  }

  @Override public HashMap<String, String> getparameters() {
    HashMap<String, String> params = new HashMap<String, String>();
    params.put("dataStatus", "");
    params.put("districtCode", "");
    params.put("customerName", "");
    LogUtil.i(params.toString());
    return params;
  }

  @Override public void resolveResponse(String arg0) {

    CustomerMainOriginal customerContacts = new Gson().fromJson(arg0, CustomerMainOriginal.class);

    setAdapter(customerContacts.getRows());
  }

  private void setAdapter(List<CustomerMainOriginal.rCustomerMain> customerDetails) {

    if (mAdapter == null) {
      mAdapter = new CustomerMultipleAdapter(getActivity(), customerDetails);
      setListAdapter(mAdapter);
    } else {

      mAdapter.clear();
      mAdapter.addAll(customerDetails);
    }
    ((CustomerMultipleAdapter) getListAdapter()).notifyDataSetChanged();
  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:
        getActivity().finish();
        return true;
      case R.id.menu_item_confirm:
        if (mAdapter != null) {
          getProductString();
        }
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  private void getProductString() {

    StringBuffer bufferName = new StringBuffer();
    StringBuffer bufferId = new StringBuffer();
    for (int i = mAdapter.getCount() - 1; i >= 0; i--) {
      CustomerMainOriginal.rCustomerMain item = mAdapter.getItem(i);
      if (mAdapter.isCheckedItem(i)) {
        if (bufferId.length() > 0) {
          bufferId.append(",").append(item.getId());
        } else {
          bufferId.append(item.getCustomerName());
        }

        if (bufferName.length() > 0) {
          bufferName.append(",").append(item.getCustomerName());
        } else {
          bufferName.append(item.getCustomerName());
        }
      }
    }

    if (bufferName == null || bufferName.length() == 0) {
      ToastUtil.show(R.string.chooseCustomerNamey);
    }
    Intent i = new Intent();
    i.putExtra(EXTRA_ID, bufferId.toString());
    i.putExtra(EXTRA_NAME, bufferName.toString());
    LogUtil.i("name===" + bufferName.toString() + ",id==" + bufferId.toString());
    getActivity().setResult(Activity.RESULT_OK, i);
    getActivity().finish();
  }
}
